package pay.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import pay.entity.form.PayAddGoodsForm;
import pay.entity.form.PayCreateOrderForm;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
@TableName("orders")
public class OrdersEntity {
    private Integer id;
    /**
     * 订单号
     */
    private String orderSn;
    /**
     * 关联预约单ID，order type 为2时关联 authenticated_game_user 表 id
     */
    private Integer reservationId;
    /**
     * 用户ID
     */
    private String userId;
    /**
     * 订单创建时间
     */
    private Date createTime;
    /**
     * 自动取消时间
     */
    private Date autoCancelTime;
    /**
     * 订单支付成功时间
     */
    private Date payTime;
    /**
     * 订单取消时间
     */
    private Date cancelTime;
    /**
     * 是否删除
     */
    private Integer del;
    /**
     * 订单状态 0 待支付 1支付成功  -1 已取消 -2 全额退款 -3 部分退款
     */
    private Integer status;
    /**
     * 订单总金额
     */
    private BigDecimal totalPrice;
    /**
     * 优惠券 ID
     */
    private Integer couponAppUserId;
    /**
     * 优惠金额
     */
    private BigDecimal couponPrice;
    /**
     * 实际支付金额
     */
    private BigDecimal payAmount;
    /**
     * 支付方式 1 微信 2 支付宝
     */
    private Integer payType;
    /**
     * 支付通道手续费
     */
    private BigDecimal channelFee;
    /**
     * 商户实收金额（用户支付金额-手续费金额）
     */
    private BigDecimal actualReceivedAmount;
    /**
     * 父订单id
     */
    private Integer belongOrderId;
    /**
     * 是否为主订单
     */
    private Integer isMain;
    /**
     * 订单类型
     * 1 常规订单
     * 2 邮费订单
     */
    private Integer orderType;
    /**
     * 邮寄商品id，使用,分割
     */
    private String postOrderGoodsId;
    /**
     * 邮寄地址id
     */
    private Integer addressId;
    /**
     * 运单编号
     */
    private String shippingNumber;
    /**
     * 物流公司名称
     */
    private String shippingCompanyName;
    /**
     * 最后一次请求订单数据的时间
     */
    private Date lastActiveTime;

    /**
     * 是否可退款
     */
    @TableField(select = false)
    private Integer canRefund;
    @TableField(select = false)
    private List<OrderGoodsEntity> orderGoodsEntityList;
    @TableField(select = false)
    private List<GoodsEntity> goodsEntityList;
    @TableField(select = false)
    private List<CouponEntity> couponEntityList;
    @TableField(select = false)
    private ContestItemUserEntity contestItemUser;
    @TableField(select = false)
    private ContestItemEntity contestItem;

    /**
     * 运动日期
     */
    @TableField(select = false)
    private Date runDate;
    @TableField(select = false)
    private List<RefundRulesEntity> refundRulesList;

    @TableField(select = false)
    private GoodsActivity goodsActivity;


    public OrdersEntity() {
    }

    public OrdersEntity(PayCreateOrderForm orderForm) {
        if (orderForm == null || orderForm.getGoodsList() == null || orderForm.getGoodsList().isEmpty()) {
            throw new RuntimeException("参数错误");
        }
        this.reservationId = orderForm.getReservationId();
        ArrayList<OrderGoodsEntity> goodsList = new ArrayList<>();
        orderForm.getGoodsList().forEach(orderGoodsEntity -> {
            OrderGoodsEntity goodsEntity = new OrderGoodsEntity();
            goodsEntity.setGoodsId(orderGoodsEntity.getGoodsId());
            goodsEntity.setCount(orderGoodsEntity.getCount());
            goodsList.add(goodsEntity);
        });
        this.orderGoodsEntityList = goodsList;
    }

    public OrdersEntity(PayAddGoodsForm form) {
        if (form == null || form.getGoodsList().isEmpty()) {
            throw new RuntimeException("参数错误");
        }
        ArrayList<OrderGoodsEntity> goodsList = new ArrayList<>();
        form.getGoodsList().forEach(orderGoodsEntity -> {
            OrderGoodsEntity goodsEntity = new OrderGoodsEntity();
            goodsEntity.setGoodsId(orderGoodsEntity.getGoodsId());
            goodsEntity.setCount(orderGoodsEntity.getCount());
            goodsList.add(goodsEntity);
        });
        this.orderGoodsEntityList = goodsList;
        this.belongOrderId = form.getOldOrderId();
    }


    @Data
    public static class GoodsActivity {
        private String banner;
        private Date startTime;
        private Date endTime;
        private String name;
        private String projectName;
        private Integer id;
        private Integer projectId;
        private String realName;
        private String phoneNumber;
        private String identityCard;
        private String allToBaseUrl;
        private Integer isStandard;
        private Integer authType;
        private Integer type;
        private Date regStartTime;
        private Date regEndTime;

        public GoodsActivity() {
        }

        // 从contest_item中获取活动
        public GoodsActivity(AuthenticatedGameEntity authenticatedGame, ContestItemEntity contestItem, ContestItemUserEntity contestItemUser) {
            assert contestItem != null;
            this.banner = authenticatedGame.getBanner().split(",")[0];
            this.startTime = authenticatedGame.getGameStart();
            this.endTime = authenticatedGame.getGameEnd();
            this.name = authenticatedGame.getName();
            this.projectName = contestItem.getName();
            this.id = authenticatedGame.getId();
            this.projectId = contestItem.getId();
            this.realName = contestItemUser.getName();
            this.phoneNumber = contestItemUser.getPhoneNumber();
            this.identityCard = contestItemUser.getIdentityCard();
            this.regStartTime = authenticatedGame.getStartTime();
            this.regEndTime = authenticatedGame.getEndTime();
            this.type = authenticatedGame.getType();
        }
    }
}
